package com.example.shopinglist;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RecyclerView myList=null;
    private int icon=R.mipmap.ic_launcher;
    String[] name=new String[]{"table","phone","apple","cake","cup"};
    String[] price=new String[]{"$1880","$6880","$0.5","$2","$3"};
    private ImageView myImage=null;
    private TextView nameText,priceNumText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myList=findViewById(R.id.myList);
        myList.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,true));
        MyAdapter myAdapter=new MyAdapter();
        myList.setAdapter(myAdapter);
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder>{

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view=View.inflate(MainActivity.this,R.layout.itemlayout,null);
            MyHolder myHolder=new MyHolder(view);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder holder, int position) {
            holder.iv.setBackgroundResource(icon);
            holder.name.setText(name[position]);
            holder.priceNum.setText(price[position]);
        }

        @Override
        public int getItemCount() {
            return name.length;
        }

        class MyHolder extends RecyclerView.ViewHolder{
            ImageView iv=null;
            TextView name,priceNum;
            public MyHolder(@NonNull View itemView) {
                super(itemView);
                iv=itemView.findViewById(R.id.picture);
                name=itemView.findViewById(R.id.name);
                priceNum=itemView.findViewById(R.id.pricenum);
            }
        }
    }

}